
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;

using Swaf;
using Swaf.BizRule;
using Swaf.BizObj;
using Swaf.Container;

namespace MediaCache
{
	public class CompanyMediaCacheBizRules: Syrinx.BaseBizRules
	{
		/// <summary>
		/// This bizrule is called by the UserLogin feature when it needs to get the list of avilable app
		/// functions for users of the application.
		/// This method returns a CSF security AppFunction bizobj list that is derived from the current user's
		/// active company's media caches.  Each media cache in the application is used to define a corisponding
		/// Security privilege which is exposed as an AppFunction.
		/// <remarks>All of the media caches for a given company become a part of a single group of
		/// application functions.  When an administrator is configuring the security access rights to various
		/// user groups each media cache will be shown in the application function list and the administrator
		/// will be able to specify the access privilege for it.  If a user is given a privilege
		/// of None for a given media cache, they will not be able to see images from it.  If a user is given
		/// a privilege of Delete, they will be able to delete the media cache, add images into it.</remarks>
		/// </summary>
		/// <param name="specifierId">The media cache AppFunction specifier id.</param>
		/// <returns></returns>
		public IBizObjList getCompanyMediaCacheAppFunctions(IApplication app, IRuleContext context, string specifierId)
		{
			try
			{
				
				if (specifierId == null)
					specifierId = "MediaCacheAppFunctions";
				FlexiMap m = new FlexiMap();
				m.put("specifierId", specifierId);
				m.put("companyId", app.securityMgr.currentPrincipal.additionalInfo.get("activeCompanyId"));
				return (IBizObjList)app.appData.get("DA.MediaCache.GetMediaCacheAppFunctions", m);
			}
			catch (Exception e)
			{
				throw new MediaCacheException("Error executing getCompanyMediaCacheAppFunctions bizRule.", e);
			}
		}


		/// <summary>
		/// This bizrule will get all of the media caches associated with the current user's active company, minus
		/// any media caches the current user does not have at least ReadOnly access to.
		/// </summary>
		/// <returns>The security filtered media caches associated with the company.</returns>
		public IBizObjList getCompanyMediaCacheLibraries(IApplication app, IRuleContext context)
		{
			try
			{
				FlexiMap m = new FlexiMap();
				m.put("companyId", ActiveCompanyId);
				IBizObjList list = app.appData.get("DA.MediaCache.GetCompanyMediaCacheLibraries", m) as IBizObjList;
				if (list != null)
				{
					Swaf.Security.ISecurityPrincipal currentPrinc = CurrentPrincipal;
					Swaf.Security.Privileges priv = Swaf.Security.Privileges.None;
					for (int pos = 0; pos < list.numElements; ++pos)
					{
						IBizObj obj = list[pos];
						priv = currentPrinc.getPrivilege("AppFunctions." + obj.get("id"));
						if (priv < Swaf.Security.Privileges.ReadOnly)
							list.removeAt(pos--);
					}
				}
				return list;
			}
			catch (Exception e)
			{
				throw new MediaCacheException("Error executing getCompanyMediaCacheLibraries.", e);
			}
		}

		/// <summary>
		/// Will provide the define media cache scalings for the current user's active company.
		/// Users of a company are assigned to a specific scaling from the company so that when they 
		/// are viewing images, the sizes and other properties of the image adjustments will be based
		/// on their assigned scaling.
		/// </summary>
		/// <returns>The Scaling bizobj list.</returns>
		public IBizObjList getCompanyScalings(IApplication app, IRuleContext context)
		{
			try
			{
				FlexiMap m = new FlexiMap();
				m.put("companyId", ActiveCompanyId);
				IBizObjList scalings = app.appData.get("DA.MediaCache.GetCompanyCacheScalings", m) as IBizObjList;
				return scalings;
			}
			catch (Exception e)
			{
				throw new MediaCacheException("Error executing getCompanyScalings.", e);
			}
		}

		public IBizObj getMediaCacheCompanySettings(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			try
			{
				IBizObj bizobj = (IBizObj)resultValue;
				if (bizobj != null)
				{
					FlexiMap m = new FlexiMap();
					m.put("companyId", bizobj.get("id"));
					IBizObjList list = app.appData.get("DA.MediaCache.GetCompanyMediaCacheLibraries", m) as IBizObjList;
					if (list.numElements > 0)
					{
                        object id = null;
                        foreach(IBizObj cache in list)
                            if ((bool)cache.get("isDefault"))
                            {
                                id = cache.get("id");
                                break;
                            }

						bizobj.put("settings.defaultMediaCache", string.Format("{0}{1}", app.appData["MediaCacheNamePrefix"], id));
						bizobj.put("settings.defaultMediaCatalogName", id);
					}
				}
				return bizobj;
			}
			catch (Exception e)
			{
				throw new MediaCacheException("error executing getCOmpanyContactDetails bizrule.", e);
			}
		}
		
	}
}
